﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cost.Models;
using NBlock.Core;

namespace Cost.Services.Services
{
    /// <summary>
    /// 库存服务类
    /// </summary>
    public class StorageService
    {
        #region 构造函数
        private readonly ICostRepository<Storage> storageRepository;
        private readonly ICostRepository<StockRecord> stockRecordRepository;
        private readonly ICostRepository<Material> materialRepository;
        public StorageService(ICostRepository<Storage> storageRepository,
            ICostRepository<StockRecord> stockRecordRepository,
            ICostRepository<Material> materialRepository)
        {
            this.storageRepository = storageRepository;
            this.stockRecordRepository = stockRecordRepository;
            this.materialRepository = materialRepository;
        }
        #endregion

        /// <summary>
        /// 入库
        /// </summary>
        /// <param name="materialId">品名Id</param>
        /// <param name="weight">入库数量</param>
        /// <param name="storageType">入库类型（如：采购入库、加工入库等）</param>
        public void StorageIn(string materialId, decimal weight, string storageType)
        {
            // 更改当前库存
            var data = storageRepository.Find(s => s.MaterialId == materialId);
            if (data != null)
            {
                data.ActualStorage += weight;
                storageRepository.Update(data);
            }
            else
            {
                var material = materialRepository.Find(materialId);
                data = new Storage
                {
                    ActualStorage = weight,
                    MaterialId = materialId,
                    Code = material.Code,
                    Name = material.Name,
                    Category = material.Category,
                    Type = material.Type,
                    Specification = material.Specification,
                    Unit = material.Unit,
                    NeedProcess = material.NeedProcess,
                    CurrentPrice = material.CurrentPrice,
                    BigClass = material.BigClass,
                };
                storageRepository.Insert(data);
            }

            //保存库存记录
            StorageLog(materialId, weight, storageType);
        }

        /// <summary>
        /// 出库
        /// </summary>
        /// <param name="materialId"></param>
        /// <param name="weight"></param>
        /// <param name="storageType"></param>
        public void StorageOut(string materialId, decimal weight, string storageType)
        {
            var data = storageRepository.Find(s => s.MaterialId == materialId);
            if (data != null)
            {
                if (data.ActualStorage < weight)
                    throw new nbValidationException(string.Format("库存（{0}）数量不足，少于出库数量（{1}）", data.Name, weight));

                data.ActualStorage -= weight;
                storageRepository.Update(data);
            }
            else
            {
                throw new nbValidationException("库存不存在，不能出库");
            }

            //保存库存记录
            StorageLog(materialId, weight, storageType);
        }

        /// <summary>
        /// 库存进出日志
        /// </summary>
        public void StorageLog(string materialId, decimal weight, string storageType)
        {
            var record = new StockRecord()
            {
                MaterialId = materialId,
                Type = storageType,
                Count = weight,
            };

            stockRecordRepository.Insert(record);
        }

        ///// <summary>
        ///// 加工单入库
        ///// </summary>
        ///// <param name="item"></param>
        //public void SaveStorageIn(MaterialProcessItem item)
        //{
        //    // 更改当前库存
        //    var Sordate = storageRepository.Find(s => s.MaterialId == item.MaterialId);
        //    if (Sordate != null)
        //    {
        //        Sordate.ActualSorage += item.StorageWeight;
        //        storageRepository.Update(Sordate);
        //    }
        //    else
        //    {
        //        var Material = materialRepository.Find(item.MaterialId);
        //        Sordate = new Storage
        //        {
        //            ActualSorage = item.StorageWeight,
        //            MaterialId = item.MaterialId,
        //            Code = item.Code,
        //            Name = item.Name,
        //            Category = item.Category,
        //            Type = "SemiMaterial",
        //            Specification = Material.Specification,
        //            Unit = Material.Unit,
        //        };
        //        storageRepository.Insert(Sordate);
        //    }

        //    //保存库存记录
        //    StockRecord StockRecorddate = new StockRecord()
        //    {
        //        Id = Guid.NewGuid().ToString(),
        //        MaterialId = item.MaterialId,
        //        Type = "加工入库",
        //        Count = item.StorageWeight,
        //    };
        //    stockRecordRepository.Insert(StockRecorddate);
        //}

        ///// <summary>
        ///// 加工单领料
        ///// </summary>
        ///// <param name="item"></param>
        //public void SaveStorageOut(MaterialProcessItem item)
        //{
        //    var Sordate = storageRepository.Find(s => s.MaterialId == item.RawMaterialId);
        //    if (Sordate != null)
        //    {
        //        Sordate.ActualSorage -= item.PickWeight;
        //        storageRepository.Update(Sordate);
        //    }

        //    //保存库存记录
        //    StockRecord StockRecorddate = new StockRecord()
        //    {
        //        Id = Guid.NewGuid().ToString(),
        //        MaterialId = item.MaterialId,
        //        Type = "加工领料",
        //        Count = item.PickWeight,
        //    };
        //    stockRecordRepository.Insert(StockRecorddate);
        //}

        ///// <summary>
        ///// 采购单入库
        ///// </summary>
        ///// <param name="item"></param>
        //public void SaveStorageIn(MaterialPurchaseItem item)
        //{
        //    //库存
        //    Storage Sordate = storageRepository.Find(s => s.MaterialId == item.MaterialId);
        //    if (Sordate == null)
        //    {
        //        Sordate = new Storage();
        //        Sordate.ActualSorage = item.StorageCount;
        //        Sordate.MaterialId = item.MaterialId;
        //        Sordate.Code = item.Code;
        //        Sordate.Name = item.Name;
        //        Sordate.Category = item.Category;
        //        var Material = materialRepository.Find(item.MaterialId);
        //        if (Material != null)
        //        {
        //            Sordate.Specification = Material.Specification;
        //            Sordate.Unit = Material.Unit;
        //        }
        //        Sordate.Type = "RawMaterial";
        //        Sordate.CurrentPrice = item.Price;
        //        storageRepository.Insert(Sordate);
        //    }
        //    else
        //    {
        //        Sordate.CurrentPrice = item.Price;
        //        Sordate.ActualSorage += item.StorageCount;
        //        storageRepository.Save(Sordate);
        //    }

        //    //保存库存记录
        //    StockRecord StockRecorddate = new StockRecord()
        //    {
        //        Id = Guid.NewGuid().ToString(),
        //        MaterialId = item.MaterialId,
        //        Type = "采购入库",
        //        Count = item.StorageCount,
        //    };
        //    stockRecordRepository.Insert(StockRecorddate);
        //}

        //public void SaveStorageOut(MaterialPurchaseItem item)
        //{

        //}

        //public void SaveStorageIn(MaterialDeliveryItem item)
        //{

        //}

        ///// <summary>
        ///// 送货
        ///// </summary>
        ///// <param name="item"></param>
        //public void SaveStorageOut(MaterialDeliveryItem item)
        //{
        //    // 更改当前库存
        //    var Sordate = storageRepository.Find(s => s.MaterialId == item.MaterialId);
        //    if (Sordate != null)
        //    {
        //        Sordate.ActualSorage -= item.DeliveryWeight;
        //        storageRepository.Update(Sordate);
        //    }

        //    //保存库存记录
        //    StockRecord StockRecorddate = new StockRecord()
        //    {
        //        Id = Guid.NewGuid().ToString(),
        //        MaterialId = item.MaterialId,
        //        Type = "送货单",
        //        Count = item.DeliveryWeight,
        //    };
        //    stockRecordRepository.Insert(StockRecorddate);
        //}
    }
}
